<template>
	<view class="content">
		<view class="uni-tabs__header">
			<view class="uni-tabs__nav-wrap">
				<view class="uni-tabs__nav-scroll">
					<scroll-view class="uni-tabs__nav" :scroll-x="true">
						<block v-for="(item, index) in menus" :key="index">
							<view @click="switchTab(item.value)" :class="{'is-active':currentTab===item.value}"
								class="uni-tabs__item">{{item.text}}</view>
						</block>
					</scroll-view>
				</view>
			</view>
		</view>
		<view v-show="currentTab==='ucharts1'">
			<qiun-title-bar :title="title" />
			<view :style="chartsbox">
				<qiun-data-charts :style="chartsbox" :ontouch="true" background="#f3f9f9" type="mount"
					:opts="{enableScroll:true,extra:{mount:{type:'bar',widthRatio:0.8}}}"
					:chartData="chartsDataRing1" />
			</view>
		</view>
		<view v-if="currentTab==='ucharts2'">
			<qiun-title-bar :title="title" />
			<view :style="chartsbox">
				<qiun-data-charts :style="chartsbox" :ontouch="true" background="#f3f9f9" type="mount"
					:opts="{enableScroll:true,extra:{mount:{type:'bar',widthRatio:0.8}}}"
					:chartData="chartsDataRing1" />
			</view>
		</view>

		<view v-if="currentTab==='echarts1'">
			<qiun-title-bar :title="title" />
			<view :style="chartsbox">
				<qiun-data-charts :style="chartsbox" :ontouch="true" background="#f3f9f9" type="mount"
					:opts="{enableScroll:true,extra:{mount:{type:'bar',widthRatio:0.8}}}"
					:chartData="chartsDataRing1" />
			</view>
		</view>
		<view v-if="currentTab==='echarts2'">
			<qiun-title-bar :title="title" />
			<view :style="chartsbox">
				<qiun-data-charts :style="chartsbox" :ontouch="true" background="#f3f9f9" type="mount"
					:opts="{enableScroll:true,extra:{mount:{type:'bar',widthRatio:0.8}}}"
					:chartData="chartsDataRing1" />
			</view>
		</view>
		<view v-if="currentTab==='echarts3'">
			<qiun-title-bar :title="title" />
			<view :style="chartsbox">
				<qiun-data-charts :style="chartsbox" :ontouch="true" background="#f3f9f9" type="mount"
					:opts="{enableScroll:true,extra:{mount:{type:'bar',widthRatio:0.8}}}"
					:chartData="chartsDataRing1" />
			</view>
		</view>
		<mx-date-picker :show="showPicker" :type="type" :value="value" :show-tips="true" :show-seconds="true"
			@confirm="ed" @cancel="ed" />
	</view>
</template>

<script>
	//下面是演示数据，您的项目不需要引用，数据需要您从服务器自行获取
	import demodata from '../../components//mockdata/demodata.json';
	import qiuntitlebar from '../../components/qiun-title-bar/qiun-title-bar.vue';
	import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
	export default {
		components: {
			qiuntitlebar,
			MxDatePicker
		},
		data() {
			return {
				chartsbox: {
					width: '100%',
					height: ''
				},
				timer: null,
				time: "10",
				title: "",
				date: this.addDate(new Date(), "0"),
				type: 'date',
				showPicker: false,
				value: '',
				sumcount:0,
				chartsDataRing1: {},
				menus: [{
					text: "边检员日报表",
					value: "ucharts1"
				}, {
					text: "机台日报表",
					value: "ucharts2"
				}, {
					text: "班组日报表",
					value: "echarts1"
				}, {
					text: "工时日报表",
					value: "echarts2"
				}, {
					text: "生产工日报表",
					value: "echarts3"
				}],
				currentTab: '', //默认选中标签
			};
		},
		mounted() {
			// 获取屏幕高度
			var then = this;
			uni.getSystemInfo({
				success(res) {
					then.chartsbox.height = (res.windowHeight * 0.82) + "px" //窗口高度，将px 转换rpx
				}
			})
		},
		onUnload() {
			if (this.timer) {
				clearTimeout(this.timer);
				this.timer = null;
			}
		},
		onHide() {
			if (this.timer) {
				clearTimeout(this.timer);
				this.timer = null;
			}
		},
		onLoad(e) {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",
				backgroundColor: this.$kdup.color
			})
			this.sumcount=e.sumcount;
			if (!this.$kdup.isEmpty(e.name)) {
				this.switchTab(e.name);
			} else {
				this.switchTab("ucharts1");
			}
			//模拟从服务器获取数据
			this.getServerData()
			//定时任务切换页签
			this.RefteshTab();
		},
		methods: {
			//定时任务
			RefteshTab() {
				this.timer = setInterval(() => {
					if (this.currentTab == "ucharts1") {
						this.switchTab("ucharts2");
					} else if (this.currentTab == "ucharts2") {
						this.switchTab("echarts1");
					} else if (this.currentTab == "echarts1") {
						this.switchTab("echarts2");
					} else if (this.currentTab == "echarts2") {
						this.switchTab("echarts3");
					} else {
						this.switchTab("ucharts1");
					}
				}, (this.time * 1000))
			},
			getServerData() {
				var Mount = {
					series: [{
						data: [],
						format: 'pieDay',
					}]
				}
				this.chartsDataRing1 = {};
				// console.log(JSON.stringify(demodata.Mount));
				//console.log(JSON.stringify(demodata.PieA));
				var data = {
					sql: this.SqlDate(this.date)
				}
				this.$myRequest.postRequest("LoadListJson", data).then(res => {
					if (res.data.error != "") {
						uni.showToast({
							title: "找不到" + this.date + "要统计的数据！",
							duration: 2000,
							icon: "none"
						});
						return;
					}
					this.sumcount=0;
					for (var i = 0; i < res.data.Table.length - 1; i++) {
						//工时分报表数据
						if (this.currentTab != "echarts2") {
							Mount.series[0].data.push({
								name: res.data.Table[i]["名称"],
								value: res.data.Table[i]["数量"]
							})
						} else {
							Mount.series[0].data.push({
								name: res.data.Table[i]["名称"],
								value: res.data.Table[i]["数量"],
								showvalue: (Math.floor(res.data.Table[i]["数量"] / 60) + "小时" + (res.data
									.Table[i]["数量"] % 60) + "分")
							})
						}
						this.sumcount+=parseInt(res.data.Table[i]["数量"]);
					}
					this.chartsDataRing1 = JSON.parse(JSON.stringify(Mount))
				})
			},
			SqlDate(date) {
				var sql = "";
				if (this.currentTab == "ucharts1") {
					sql =
						"/*dialect*/select  u.FNAME 名称,SUM(cast(g1.FHBQTY as float)) 数量 from top_t_HBGX g inner join top_t_HBGXEntry g1 on g.FID=g1.FID inner join top_t_Cust_Entry100011 p " +
						" on p.FBTPCBILLNO=g1.FSRCPCBTBNO inner join T_SEC_USER u     on g.FCREATORID=u.FUSERID    " +
						" where p.FBTPCBILLNO!='' and p.FGX!=''    and Convert(varchar(10),g.FCREATEDATE,121)='" + date +
						"' " +
						"  group by u.FNAME  order by  SUM(cast(g1.FHBQTY as float))  desc ";
				} else if (this.currentTab == "ucharts2") {
					sql =
						"/*dialect*/select  j.FNAME 名称,SUM(cast(g1.FHBQTY as float)) 数量 from top_t_HBGX g inner join top_t_HBGXEntry g1 on g.FID=g1.FID inner join top_t_Cust_Entry100011 p " +
						" on p.FBTPCBILLNO=g1.FSRCPCBTBNO   inner join KDUP_t_JTSJ_L j on j.FID=g1.F_TOP_JTBH " +
						"  where p.FBTPCBILLNO!='' and p.FGX!=''   and Convert(varchar(10),g.FCREATEDATE,121)='" +
						date + "' " +
						"  group by j.FNAME   order by  SUM(cast(g1.FHBQTY as float))  desc";
				} else if (this.currentTab == "echarts1") {
					sql =
						"/*dialect*/select  t.FNAME 名称,SUM(cast(g1.FHBQTY as float)) 数量 from top_t_HBGX g inner join top_t_HBGXEntry g1 on g.FID=g1.FID inner join top_t_Cust_Entry100011 p " +
						" on p.FBTPCBILLNO=g1.FSRCPCBTBNO  inner join T_PRD_SHIFTGROUP_L t   on t.FID=p.FPB   where p.FBTPCBILLNO!='' and p.FGX!='' " +
						"   and Convert(varchar(10),g.FCREATEDATE,121)='" + date + "' " +
						" group by t.FNAME  order by t.FNAME ";
				} else if (this.currentTab == "echarts2") {
					sql =
						"/*dialect*/select  e.FNAME 名称,SUM(DATEDIFF(MI,g.FSTARTDATETIME,g.FENDDATETIME))AS  数量 from top_t_HBGX g inner join top_t_HBGXEntry g1 on g.FID=g1.FID inner join top_t_Cust_Entry100011 p on p.FBTPCBILLNO=g1.FSRCPCBTBNO inner join T_SEC_USER e  on g.FCREATORID=e.FUSERID" +
						"  where p.FBTPCBILLNO!='' and p.FGX!=''    and Convert(varchar(10),g.FCREATEDATE,121)='" + date +
						"' " +
						"  group by e.FNAME order by  SUM(DATEDIFF(MI,g.FSTARTDATETIME,g.FENDDATETIME))  desc ";
				} else {
					sql =
						"/*dialect*/select  e.FNAME 名称,SUM(cast(g1.FHBQTY as float)) 数量 from top_t_HBGX g inner join top_t_HBGXEntry g1 on g.FID=g1.FID inner join top_t_Cust_Entry100011 p " +
						" on p.FBTPCBILLNO=g1.FSRCPCBTBNO inner join T_HR_EMPINFO_L e  on p.FEMPLOYEE=e.FID    " +
						" where p.FBTPCBILLNO!='' and p.FGX!=''    and Convert(varchar(10),g.FCREATEDATE,121)='" + date +
						"' " +
						"  group by e.FNAME  order by  SUM(cast(g1.FHBQTY as float))  desc ";
				}
				return sql;
			},
			switchTab(tab) {
				if (tab == "ucharts1") {
						this.title = "机台日报表-" + this.date+(this.sumcount==0?"":",总数："+this.sumcount);
				} else if (tab == "ucharts2") {
					this.title = "机台日报表-" + this.date+",总数："+this.sumcount;
				} else if (tab == "echarts1") {
					this.title = "班组日报表-" + this.date+",总数："+this.sumcount;
				} else if (tab == "echarts2") {
					this.title = "工时日报表-" + this.date;
				} else {
					this.title = "生产工日报表-" + this.date+",总数："+this.sumcount;
				}
				this.currentTab = tab
				this.getServerData();
			},
			DatePicker(type) { //显示
				this.type = type;
				this.showPicker = true;
				this.value = this[type];
			},
			ed(e) { //选择
				this.showPicker = false;
				if (e) {
					this[this.type] = e.value;
					this.date = e.value;
					this.switchTab(this.currentTab)
					this.getServerData();
				}
			},
			onNavigationBarButtonTap(e) {
				this.DatePicker('date');
			},
			//获取今天日期，格式YYYY-MM-DD
			addDate(date, days) {
				if (days == undefined || days == '') {
					days = 1;
				}
				var date = new Date(date);
				date.setDate(date.getDate() - days);
				var month = date.getMonth() + 1;
				var day = date.getDate();
				var mm = "'" + month + "'";
				var dd = "'" + day + "'";

				//单位数前面加0
				if (mm.length == 3) {
					month = "0" + month;
				}
				if (dd.length == 3) {
					day = "0" + day;
				}
				var time = date.getFullYear() + "-" + month + "-" + day
				return time;
			}
		}
	};
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		flex: 1;
		width: 100%;
	}
</style>
